package org.achao.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.achao.mapper.MybatisPlusDemoMapper;
import org.achao.pojo.MybatisPlusDemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author achao
 * @description mybatis plus 控制层
 * @date 2023/9/23 16:37
 */

@RestController
@RequestMapping("/mybatis")
public class MybatisPlusDemoController {

    @Autowired
    private MybatisPlusDemoMapper mybatisPlusDemoMapper;

    @GetMapping("/selectAll")
    private List<MybatisPlusDemo> selectAll() {
        return mybatisPlusDemoMapper.selectList(null);
    }

    @GetMapping("/selectByKey")
    private List<MybatisPlusDemo> selectByKey(String key) {
        Page<MybatisPlusDemo> page = new Page<>(1, 3);
        Page<MybatisPlusDemo> mybatisPlus = mybatisPlusDemoMapper.selectPageVo(page, key);
        return mybatisPlus.getRecords();
    }

    @PostMapping("/insert")
    private String insert(@RequestBody MybatisPlusDemo mybatisPlusDemo) {
        return mybatisPlusDemoMapper.insert(mybatisPlusDemo) > 0 ? "新增成功" : "新增失败";
    }
}
